package com.qire.manhua.dal.service;

import com.qire.antsbinder.dal.DataService;
import com.qire.antsbinder.dal.annotation.StrategyAnnotation;
import com.qire.common.basic.ObjectUtil;
import com.qire.common.constant.Url;
import com.qire.common.dal.WarehouseFactory;
import com.qire.common.model.enumeration.VIPType;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.snackbar.Prompt;
import com.qire.manhua.dal.ICommentDataDAL;
import com.qire.manhua.model.entity.ComicBookEntity;
import com.qire.manhua.model.viewModel.impl.MessageCommentDetailViewModel;
import com.qire.manhua.model.vo.CommentReplyVO;
import com.qire.manhua.model.vo.MessageCommentDetailVO;
import com.qire.manhua.model.vo.OkResponseVO;

import java.util.List;

public class MessageCommentDetailService extends DataService {

    private final ICommentDataDAL commentDataDAL = WarehouseFactory.create(this, ICommentDataDAL.class);

    private final MessageCommentDetailViewModel viewModel;

    public MessageCommentDetailService(MessageCommentDetailViewModel viewModel) {
        this.viewModel = viewModel;
    }

    /**
     * 加载评论详情数据
     * @param commentId 评论ID
     */
    public void loadCommentDetail(int commentId) {
        commentDataDAL.fetchMessageCommentDetail(commentId);
    }
    @StrategyAnnotation(event = Url.CommentDetailData)
    public void fetchMessageCommentDetailApiCallback(MessageCommentDetailVO dataVO, boolean isCache) {
        if(ObjectUtil.isNull(dataVO)) {
            return;
        }

        final int comicId = dataVO.book.id;
        final String comicName = dataVO.book.book_name;

        final int commentId = dataVO.id;
        final String userName = dataVO.user_avatar.user_name;
        final String userPortrait = dataVO.user_avatar.user_img;
        final CharSequence commentContent = dataVO.content;
        final CharSequence commentTime = dataVO.create_time;
        final VIPType vipType = VIPType.build(dataVO.user_avatar.vipType);
        final boolean isMyLike = dataVO.like == 1;
        final int commentLikeCount = dataVO.like_count;
        final int commentReplyCount = dataVO.replie_count;

        viewModel.setReferRelatedData(
                new ComicBookEntity(comicId, comicName),
                viewModel.buildCommentItem(commentId, userName, userPortrait, commentContent,
                        commentTime, vipType, isMyLike, commentLikeCount, commentReplyCount)
        );
    }

    /**
     * 加载回复评论列表数据
     * @param comicBookId 漫画ID
     * @param commentId 目标评论ID
     * @param pageNo 页号
     */
    public void loadReplyCommentListData(int comicBookId, int commentId, int pageNo) {
        commentDataDAL.fetchReplyCommentList(comicBookId, commentId, pageNo);
    }
    @StrategyAnnotation(event = Url.comment_reply_list)
    public void fetchReplyCommentListApiCallBack(List<CommentReplyVO> dataVOList, boolean isCache) {

        viewModel.refreshLayoutFinish(300);

        if(ObjectUtil.isNull(dataVOList)) {
            return;
        }

        if (dataVOList.isEmpty()) {
            viewModel.noMoreDataWasShown();
            return;
        }

        for(CommentReplyVO replyVO : dataVOList) {

            final int commentId = replyVO.id;
            final String userName = replyVO.user_avatar.user_name;
            final String userPortrait = replyVO.user_avatar.user_img;
            final CharSequence commentContent = replyVO.buildReplyContent();
            final CharSequence commentTime = replyVO.create_time;
            final VIPType vipType = VIPType.build(replyVO.user_avatar.vipType);
            final boolean isMyLike = replyVO.like == 1;
            final int commentLikeCount = replyVO.like_count;
            final int commentReplyCount = replyVO.replie_count;

            viewModel.addReplyCommentItem(viewModel.buildCommentItem(commentId, userName, userPortrait,
                    commentContent, commentTime, vipType, isMyLike, commentLikeCount, commentReplyCount));
        }

        viewModel.refreshMessageReplyAdapter();

        if (!isCache) {
            viewModel.nextPage();
        }
    }

    /** 点赞给喜欢的评论*/
    public void voteToGiveLikeComment(int bookId, int commentId) {
        commentDataDAL.voteToGiveLikeComment(bookId, commentId);
    }
    @StrategyAnnotation(event = Url.comment_like)
    public void voteToGiveLikeCommentApiCallBack(OkResponseVO dataVO, boolean isCache) {
        ToastHelper.showSnackBar(dataVO.ok, Prompt.SUCCESS);
    }

    /**
     * 举报评论
     * @see ICommentDataDAL#complaintComment(int, int, int, String)
     */
    public void complaintComment(int bookId, int commentId, int reportType, String content) {
        commentDataDAL.complaintComment(bookId, commentId, reportType, content);
    }
    @StrategyAnnotation(event = Url.comment_report)
    public void complaintCommentApiCallBack(OkResponseVO dataVO, boolean isCache) {
        if(dataVO.isOK()) {
            ToastHelper.showSnackBar("举报成功", Prompt.SUCCESS);
        } else {
            ToastHelper.showSnackBar("举报失败", Prompt.ERROR);
        }
    }


    /**
     * 回复评论
     * @param comicId 漫画ID
     * @param replyToTargetId 回复的目标评论ID
     * @param replyContent 回复的评论内容
     */
    public void replyComment(int comicId, int replyToTargetId, String replyContent) {
        commentDataDAL.submitComment(comicId, 0, replyToTargetId, replyContent, 5);
    }
    @StrategyAnnotation(event = Url.comment_add)
    public void replyCommentApiCallback(OkResponseVO dataVO, boolean isCache) {
        ToastHelper.showSnackBar("评论成功", Prompt.SUCCESS);
        viewModel.refreshReplyUI();
        viewModel.loadReplyData();
    }

}
